import { Component, EventEmitter, OnChanges, OnInit, SimpleChanges, Input, Output } from '@angular/core';

@Component({
  selector: 'el-year-table',
  templateUrl: './year-table.component.html',
})
export class YearTableComponent implements OnInit, OnChanges {
  @Input() showWeekNumber: boolean = false;
  @Input() model: number = 0;
  @Input('disabled-date') disabledDate: any;
  @Output() modelChange: EventEmitter<number> = new EventEmitter();
  @Input() date!: Date;
  @Input() yearRows: Array<any> = [];
  @Input() currentYear: number = 0;

  constructor() { }

  clickHandle(year: number): void {
    this.currentYear = year;
    this.date.setFullYear(year);
    this.modelChange.emit(this.date.getTime());
  };


  updateYearRow(currentYear: number): any {
    var /** @type {?} */ startYear = ~~(currentYear / 10) * 10;
    return [[], [], []].map(function (v, index) {
      return [0, 1, 2, 3].map(function (num) { return startYear + (index * 4) + num; });
    });
  };

  ngOnInit(): void {
    this.date = new Date(this.model);
    this.currentYear = this.date.getFullYear();
    this.yearRows = this.updateYearRow(this.currentYear);
  };

  ngOnChanges(changes: SimpleChanges): void {
    if (!changes || !changes.model)
      return;
    // first change
    if (changes.model.isFirstChange())
      return;
    this.model = changes.model.currentValue;
    this.date = new Date(this.model);
    this.currentYear = this.date.getFullYear();
    this.yearRows = this.updateYearRow(this.currentYear);
  };

}
